package task1and3;

public class ToStringOverrideTest {

    public static String toString(int[] arr) {
        //如果int类型数组arr为null，toString方法返回字符串“null”
        if(null == arr) return "null";
        //如果int类型数组arr长度为0，toString方法返回字符串“[]”
        if(0 == arr.length) return "[]";
        //如果int类型数组arr的内容为{1,2,3,4,5}，toString方法返回字符串“[1, 2, 3, 4, 5]”
        StringBuilder sb1 = new StringBuilder();
        sb1.append("[");
        for(int i = 0; i <arr.length; i++) {
            sb1.append(arr[i]);
            if(arr.length-1 == i) {
                sb1.append("]");
            } else {
                sb1.append(", ");
            }
        }
        return sb1.toString();
    }

    public static void main(String[] args) {
        int[] arr1 = null;
        int[] arr2 = new int[0];
        int[] arr3 = new int[]{1, 2, 3, 4, 5};
        String s1 = toString(arr1);
        String s2 = toString(arr2);
        String s3 = toString(arr3);
        System.out.println(s1);
        System.out.println(s2);
        System.out.println(s3);
    }
}
